package com.yixun.lbx.controller.farm;

import com.yixun.controller.BaseController;
import com.yixun.lbx.entity.YxProBreedDisinfect;
import com.yixun.lbx.entity.YxProCompany;
import com.yixun.lbx.service.BreedDisinfectService;
import com.yixun.lbx.service.CompanyService;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/bd")
public class BreedDisinfectController extends BaseController {
    private final BreedDisinfectService disinfectService;
    private final CompanyService companyService;

    public BreedDisinfectController(CompanyService companyService, BreedDisinfectService disinfectService) {
        this.companyService = companyService;
        this.disinfectService = disinfectService;
    }

    /*
     *获取消毒记录列表
     */
    @PostMapping("/getListByPage")
    public HashMap<String, Object> getListByPage(@RequestBody HashMap<String, Object> map) {
        Long qyId = getQyId();
        if (qyId != 0L) {
            map.put("qyId", qyId);
        }
        Integer count = disinfectService.getCount(map);
        List<YxProBreedDisinfect> disinfects = disinfectService.getListByPage(map);

        map.put("count", count);
        map.put("disinfects", disinfects);
        return map;
    }

    /*
     *添加消毒记录
     */
    @PostMapping("/addDisinfect")
    public int addDisinfect(@RequestBody HashMap<String, Object> map) {
        Long qyId = getQyId();
        if (qyId != 0L) {
            map.put("qyId", qyId);
        }
        map.put("addUser", this.getSessionUser().getUser_id());
        return disinfectService.addDisinfect(map);
    }

    /*
     *修改消毒记录
     */
    @PostMapping("/updateDisinfect")
    public int updateDisinfect(@RequestBody HashMap<String, Object> map) {
        map.put("updateUser", this.getSessionUser().getUser_id());
        return disinfectService.updateDisinfect(map);
    }

    /*
     *删除消毒记录
     */
    @PostMapping("/deleteDisinfect")
    public int deleteDisinfect(@RequestBody HashMap<String, Object> map) {
        map.put("updateUser", this.getSessionUser().getUser_id());
        String[] ids = map.get("id").toString().split(",");
        map.put("ids", ids);
        return disinfectService.deleteDisinfect(map);
    }

    @GetMapping("/getObjByArgs")
    public List<YxProBreedDisinfect> getObjByArgs(YxProBreedDisinfect disinfect) {
        return disinfectService.getObjByArgs(disinfect);
    }

    /*搜索*/
    @RequestMapping("/getSearchListByPage")
    public HashMap<String, Object> getSearchListByPage(@RequestBody HashMap<String, String> map) {
        Long qyId = getQyId();
        if (qyId != 0L) {
            map.put("qyId", String.valueOf(qyId));
        }
        List<YxProBreedDisinfect> objs = disinfectService.getSearchListByPage(map);
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("obj", objs);
        hashMap.put("count", objs.size());
        return hashMap;
    }

    private Long getQyId() {
        YxProCompany yxProCompany = new YxProCompany();
        yxProCompany.setCode(this.getSessionUser().getUser_name());
        YxProCompany company = companyService.getCompanyByIdOrNameOrCode(yxProCompany);
        if (company != null) {
            return company.getQyId();
        } else {
            return 0L;
        }
    }
}
